﻿using System;

namespace MikeKappel.Com.CS
{
    public static partial class SortingAlgorithms
    {
        public delegate Boolean SelectionDelegate(Int32 currentIndex, Int32 currentValue);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="onSort">On value change method to fire</param>
        /// <param name="ittems">Array of Integers to sort</param>
        public static void SelectionSort(Int32[] items, SelectionDelegate onSort)
        {
            for (Int32 i = 0; i < items.Length - 1; i++)
            {
                Int32 minimum = i;
                for (Int32 i2 = i + 1; i2 < items.Length; i2++)
                {
                    if (onSort != null && !onSort(i2, items[i2]))
                        goto escape;

                    if (items[i2] < items[minimum])
                        minimum = i2;
                }
                Int32 temp = items[i];
                items[i] = items[minimum];
                if (onSort != null && !onSort(i, items[i]))
                    goto escape;
                items[minimum] = temp;
                if (onSort != null && !onSort(minimum, items[minimum]))
                    goto escape;
            }
        escape:
            return;
        }
        public static void SelectionSort(Int32[] items)
        {
            SelectionSort(items, null);
        }
    }
}
